﻿Imports System.Reflection

''' <summary>
''' 子関連
''' </summary>
''' <remarks>属性ではありません</remarks>
Public Class ChildRelation

    Public Sub New(prop As PropertyInfo, yourColumn As ParentRelationColumnAttribute)
        _collectionProperty = prop
        _childType = prop.PropertyType.GetGenericArguments.First
        _yourColumn = yourColumn
    End Sub

    ''' <summary>
    ''' テーブル属性
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Friend Property Table As TableAttribute

    Private _collectionProperty As PropertyInfo
    ''' <summary>
    ''' 子アイテムを格納するコレクションプロパティ
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ReadOnly Property CollectionProperty As PropertyInfo
        Get
            Return _collectionProperty
        End Get
    End Property

    Private _childType As Type
    ReadOnly Property ChildType As Type
        Get
            Return _childType
        End Get
    End Property

    Public ReadOnly Property MyColumn As ColumnAttribute
        Get
            Return Me.YourColumn.YourColumn
        End Get
    End Property

    Private _yourColumn As ParentRelationColumnAttribute
    ''' <summary>
    ''' 相手の結合列
    ''' </summary>
    ''' <value></value>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public ReadOnly Property YourColumn As ParentRelationColumnAttribute
        Get
            Return _yourColumn
        End Get
    End Property

    ''' <summary>
    ''' 2つのインスタンスが結合できるかどうかを示します
    ''' </summary>
    ''' <param name="myInstance"></param>
    ''' <param name="yourInstance"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function CanJoin(myInstance As Object, ByVal yourInstance As Object) As Boolean
        If myInstance Is Nothing OrElse yourInstance Is Nothing Then Return False

        Dim myKey As Object = Me.MyColumn.GetValue(myInstance)
        Dim yourKey As Object = Me.YourColumn.GetValue(yourInstance)

        Return Comparer.Equals(myKey, yourKey)
    End Function

End Class
